package org.nlpserver;

import com.sun.jna.Library;
import com.sun.jna.Native;

import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;

import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;

public class Participle {

    private String participleResult;
    private String keyWords;

    public String getParticipleResult() {
        return this.participleResult;
    }

    public String getKeywords() {
        return this.keyWords;
    }

    public interface CLibrary extends Library {
        // 定义并初始化接口的静态变量
        CLibrary Instance = (CLibrary) Native.loadLibrary(
                "C:\\Users\\onetwo\\Desktop\\LearnJava\\libs\\nlpir2014\\win64\\NLPIR",
                CLibrary.class
        );

        public int NLPIR_Init(String sDataPath, int encoding,
                              String sLicenceCode);

        public String NLPIR_ParagraphProcess(String sSrc, int bPOSTagged);

        public String NLPIR_GetKeyWords(String sLine, int nMaxKeyLimit,
                                        boolean bWeightOut);

        public String NLPIR_GetLastErrorMsg();

        public void NLPIR_Exit();
    }


    public Participle parse(String stringToParticiple, String dataRoot) {





        // String system_charset = "GBK";//GBK----0
        String system_charset = "UTF-8";
        int charset_type = 1;

        int init_flag = CLibrary.Instance.NLPIR_Init(dataRoot, charset_type, "0");
        String nativeBytes = null;

        if (0 == init_flag) {
            nativeBytes = CLibrary.Instance.NLPIR_GetLastErrorMsg();
            throw new Error("初始化失败！fail reason is " + nativeBytes);
        }

        //String nativeBytes = null;
        try {
            nativeBytes = CLibrary.Instance.NLPIR_ParagraphProcess(stringToParticiple, 1);

            this.participleResult = nativeBytes;

            String nativeByte = CLibrary.Instance.NLPIR_GetKeyWords(stringToParticiple, 10, false);

            this.keyWords = nativeByte;

            CLibrary.Instance.NLPIR_Exit();


        } catch (Exception ex) {
            throw new RuntimeException("看来分析程序出了点问题", ex);
        }

        return this;
    }
}
